package controller

import (
	"context"
	"github.com/RaymondCode/simple-demo/config"
	"github.com/RaymondCode/simple-demo/entity/myerr"
	"github.com/RaymondCode/simple-demo/entity/param"
	"github.com/RaymondCode/simple-demo/entity/response"
	"github.com/RaymondCode/simple-demo/middleware"
	serviceimpl "github.com/RaymondCode/simple-demo/service/impl"
	"github.com/RaymondCode/simple-demo/util/webutil"
	"github.com/cloudwego/hertz/pkg/app"
	"log"
	"net/http"
	"strconv"
)

var relationService = serviceimpl.NewRelationServiceInstance()

func Follow(ctx context.Context, c *app.RequestContext) {
	var relationParam param.Relation
	var err error
	relationParam.ActionType, err = strconv.Atoi(c.Query("action_type"))
	relationParam.ToUserID, err = strconv.Atoi(c.Query("to_user_id"))
	if err != nil {
		log.Println(err)
		c.JSON(http.StatusBadRequest, response.ArgumentError(myerr.ArgumentInvalid(webutil.GetValidMsg(err, relationParam))))
		return
	}
	userId, err := strconv.Atoi(middleware.ThreadLocal.Get().(map[string]string)[config.Config.ThreadLocal.Keys.UserId])
	if err != nil {
		log.Println(err)
		c.JSON(http.StatusInternalServerError, response.SystemError)
		return
	}
	err = relationService.Follow(&relationParam, userId)
	if err != nil {
		log.Println(err)
		c.JSON(http.StatusForbidden, response.SystemError)
		return
	}
	c.JSON(http.StatusOK, response.Ok)
}

func FollowList(ctx context.Context, c *app.RequestContext) {
	var followListParam param.FollowList
	var err error
	followListParam.UserID, err = strconv.Atoi(c.Query("user_id"))
	if err != nil {
		log.Println(err)
		c.JSON(http.StatusInternalServerError, response.SystemError)
		return
	}
	userList, err := relationService.FollowList(followListParam.UserID)
	if err != nil {
		log.Println(err)
		c.JSON(http.StatusForbidden, response.SystemError)
		return
	}
	c.JSON(http.StatusOK, response.FollowList{
		Response: response.Ok,
		Data:     *userList,
	})
}

func FansList(ctx context.Context, c *app.RequestContext) {
	var fansListParam param.FollowList
	var err error
	fansListParam.UserID, err = strconv.Atoi(c.Query("user_id"))
	if err != nil {
		log.Println(err)
		c.JSON(http.StatusInternalServerError, response.SystemError)
		return
	}
	fansList, err := relationService.FansList(fansListParam.UserID)
	if err != nil {
		log.Println(err)
		c.JSON(http.StatusForbidden, response.SystemError)
		return
	}
	c.JSON(http.StatusOK, response.FansList{
		Response: response.Ok,
		Data:     *fansList,
	})
}

func FriendList(ctx context.Context, c *app.RequestContext){
	var fansListParam param.FollowList
	var err error
	fansListParam.UserID, err = strconv.Atoi(c.Query("user_id"))
	if err != nil {
		log.Println(err)
		c.JSON(http.StatusInternalServerError, response.SystemError)
		return
	}
	friendList, err := relationService.FriendList(fansListParam.UserID)
	if err != nil{
		log.Println(err)
		c.JSON(http.StatusForbidden, response.SystemError)
		return
	}
	c.JSON(http.StatusOK, response.FansList{
		Response: response.Ok,
		Data:     *friendList,
	})
}
